#ifndef __REAL_H__
#define __REAL_H__

#include<iostream>
#include<string>

class Real
{

public:
	Real();
	~Real();

	// Casteo de double a Real de t dígitos
	Real( double n );

	// Operadores con reales
	Real  operator+ ( const Real& r ) const;
	Real  operator- ( const Real& r ) const;
	Real  operator* ( const Real& r ) const;
	Real  operator/ ( const Real& r ) const;
	Real& operator+=( const Real& r );
	Real& operator-=( const Real& r );
	Real& operator*=( const Real& r );
	Real& operator/=( const Real& r );

	// Operadores con parámetros invertidos
	friend Real operator/ ( const double n, const Real& r );

	// Devuelve el número representado
	double toDouble() const;

	static bool roundNumber;
	static void precision( unsigned int t );
	static unsigned int precision();

	// Función auxiliar que devuelve el binario del número representado
	std::string toBin() const
	{
		char* c = (char*)&number;
		std::string out;
		for( int i = 7 ; i >= 0 ; i-- )
			out += Real::toBin( c[i] ) + ' ';
		return out;
	}

private:

	double number;

	static double truncate( double n );
	static double round( double n );
	static unsigned int digits;

	static std::string toBin( unsigned char x )
	{
		std::string out;
		unsigned char pot = 128;
		while( pot > 0 )
		{
    	    out += '0' + (x / pot) % 2;
	        pot /= 2;			
		}
		return out;
	}

};

// Operadores extras
Real operator/ ( const double n, const Real& r );
Real operator* ( const double n, const Real& r );

std::ostream& operator<< ( std::ostream& out, const Real& r );

#endif

